package com.showcai.controller.media;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.AdminUser;
import com.showcai.module.MediaClassify;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.MediaClassifyService;
import com.showcai.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-15.
 */
@RequestMapping("/mediaClassify")
@Controller
public class MediaClassifyController extends BaseController {
    @Autowired
    MediaService mediaService;
    @Autowired
    MediaClassifyService mediaClassifyService;

    /**
     * 获取专辑列表
     *
     * @return
     */
    @RequestMapping("/mediaClassifyList")
    public ModelAndView mediaClassifyList() {
        ModelAndView m = new ModelAndView("/media/mediaClassifyList");
        Map<String, String> map = super.showRequestParameters();

        PageInfo<MediaClassify> pageInfo = mediaClassifyService.selectMediaClassifyList(map);
        List<MediaClassify> mediaClassifyList = mediaClassifyService.getMediaClassify(map);
        m.addObject("pageInfo", pageInfo);
        m.addObject("mediaClassifyList",mediaClassifyList);
        m.addObject("map", map);
        return m;
    }
    @RequestMapping("/getMediaClassifyList")
    @ResponseBody
    public String getMediaClassifyList() {
        ResultObj resultObj = new ResultObj();
        List<MediaClassify> mediaClassifyList = mediaClassifyService.getMediaClassify(new HashMap<>());
        resultObj.setData(mediaClassifyList);
        return resultObj.toJson();
    }
    /**
     * 媒体页面
     *
     * @return
     */
    @RequestMapping("/mediaClassify")
    public ModelAndView mediaClassify() {
        ModelAndView m = new ModelAndView("/media/mediaClassify");
        Map<String, String> map = super.showRequestParameters();

        MediaClassify mediaClassify = mediaClassifyService.selectMediaClassifyByTid(map);
        if(mediaClassify!=null){
            map.put("tid",String.valueOf(mediaClassify.getParentId()));
            MediaClassify parentMediaClassify=mediaClassifyService.selectMediaClassifyByTid(map);
            m.addObject("parentMediaClassify", parentMediaClassify);

        }
        m.addObject("mediaClassify", mediaClassify);
        m.addObject("map", map);
        return m;
    }



    /**
     * 更新媒体分类
     *
     * @param mediaClassify
     * @return
     */
    @RequestMapping(value = "/modifyMediaClassify")
    @ResponseBody
    public String modifyMedia(MediaClassify mediaClassify, @RequestParam(value = "pic", required = false) MultipartFile pic) {
        ResultObj resultObj = new ResultObj();
        String mediaClassifyPicUrl = "";
        String mediaUrl = "";
        String filePath = "showcai/media";
        try {
            if (pic != null) {
                try {
                    mediaClassifyPicUrl = upLoadFile(pic.getInputStream(), filePath);
                    logger.info("resourceUrl=============>>>" + mediaClassifyPicUrl);
                    mediaClassify.setPicUrl(mediaClassifyPicUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            mediaClassify.setOperatorId(((AdminUser)session.getAttribute(Constant.SESSION_KEY_ADMIN_USER_LOGIN)).getTid());
            mediaClassifyService.modifyMediaClassify(mediaClassify);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 获取媒体分类
     * @return
     */
    @RequestMapping(value = "/getMediaClassify")
    @ResponseBody
    public String getMediaClassify() {
        ResultObj resultObj = new ResultObj();
        try {
           resultObj.setData(getMediaClassifyOBJ(0l,new ArrayList<MediaClassify>()));
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    private List<MediaClassify> getMediaClassifyOBJ(Long parentId, List<MediaClassify> mediaClassifylist) {
        mediaClassifylist = mediaClassifyService.getMediaClassify(new HashMap<String, String>() {{
            put("parentId", String.valueOf(parentId));
        }});

        for (MediaClassify mediaClassify : mediaClassifylist
                ) {
            List<MediaClassify> mediaClassifylistTemp = getMediaClassifyOBJ(mediaClassify.getTid(), new ArrayList<MediaClassify>());
            if (mediaClassifylistTemp.size() > 0) {
                mediaClassify.setMediaClassifyList(mediaClassifylistTemp);
            }
        }
        return mediaClassifylist;
    }

}
